package com.sekill.miaosha.mq;


import com.alibaba.fastjson.JSON;
import com.seckill.system.api.RemoteOrderService;
import com.sekill.miaosha.mapper.TbStockMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Component
/*@RabbitListener(bindings = @QueueBinding(
		value = @Queue(value = "queue.Exchange", autoDelete = "false"),
		exchange = @Exchange(value = "seckill.queue", type = ExchangeTypes.DIRECT, durable = "true"),
		key = "queue"))*/
public class MQReceiver {

		private static Logger log = LoggerFactory.getLogger(MQReceiver.class);

		@Autowired
		TbStockMapper tbStockMapper;

		@Autowired
		RemoteOrderService remoteOrderService;

		@RabbitHandler
		public void receive(@Payload String message) {
			/*log.info("receive message:"+message);
			SeckillMessage mm = stringToBean(message, SeckillMessage.class);

			Order order = new Order();
			order.setTotalPay(mm.getSeckillPrice().longValue());
			order.setActualPay(mm.getSeckillPrice().longValue());
			order.setUserId(mm.getUserid().toString());
			remoteOrderService.save(order);

			tbStockMapper.updatestock(mm.getSkuid());*/
		}

	public static <T> T stringToBean(String str, Class<T> clazz) {
		if(str == null || str.length() <= 0 || clazz == null) {
			return null;
		}
		if(clazz == int.class || clazz == Integer.class) {
			return (T)Integer.valueOf(str);
		}else if(clazz == String.class) {
			return (T)str;
		}else if(clazz == long.class || clazz == Long.class) {
			return  (T)Long.valueOf(str);
		}else {
			return JSON.toJavaObject(JSON.parseObject(str), clazz);
		}
	}
}
